package com.foreverwin.mes.production.controller;

import com.alibaba.fastjson.JSONObject;
import com.foreverwin.mes.core.base.FrontPage;
import com.foreverwin.mes.core.base.AjaxResult;
import com.foreverwin.mes.core.exception.BusinessException;
import com.foreverwin.mes.core.utils.CommonMethods;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.foreverwin.mes.core.utils.StringUtils;
import com.foreverwin.mes.production.dto.CleanExeDto;
import com.foreverwin.mes.production.dto.CleanExeReviewDto;
import com.foreverwin.mes.production.model.CleanData;
import com.foreverwin.mes.production.model.CleanDataItem;
import com.foreverwin.mes.production.model.CleanExeItem;
import com.foreverwin.mes.production.service.CleanDataItemService;
import com.foreverwin.mes.production.service.CleanDataService;
import com.foreverwin.mes.production.service.CleanExeItemService;
import com.foreverwin.mes.production.vo.CleanExeReviewVo;
import com.foreverwin.mes.production.vo.CleanExeVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.foreverwin.mes.production.service.CleanExeService;
import com.foreverwin.mes.production.model.CleanExe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 *
 * @author Max
 * @since 2020-02-11
 */
@Controller
@RequestMapping("/cleanExes")
public class CleanExeController {
    private final Logger logger = LoggerFactory.getLogger(CleanExeController.class);

    @Autowired
    public CleanExeService cleanExeService;
    @Autowired
    public CleanDataService cleanDataService;
    @Autowired
    public CleanDataItemService cleanDataItemService;
    @Autowired
    public CleanExeItemService cleanExeItemService;


    /**
    * 根据id查询
    *
    * @param id 主键
    * @return
    */
    @ResponseBody
    @GetMapping("/{id:.+}")
    public AjaxResult getCleanExeById(@PathVariable String id) {
        CleanExe result;
        try {
            result = cleanExeService.selectById(id);
        } catch (Exception e) {
            logger.error("getCleanExeById -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(result);
    }

    /**
     * 查询任务或清洁明细
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public AjaxResult getCleanExeList(CleanData cleanData){
        CleanExeVo result;
        try {
            result = cleanExeService.result(cleanData);
        } catch (Exception e) {
            return AjaxResult.error(StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage());
        }
        return AjaxResult.success(result);
    }

    /**
     * 任务开始
     * @return
     */
    @ResponseBody
    @GetMapping("/startTask")
    public AjaxResult startCleanExe(CleanData cleanData){
        CleanExeVo result = null;
        try {
            result = cleanExeService.startResult(cleanData);
        } catch (Exception e) {
            return AjaxResult.error(StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage());
        }
        return AjaxResult.success(result);
    }

    /**
     * 任务完成
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/endTask")
    public AjaxResult endCleanExe(@RequestBody CleanExeDto cleanExeDto){
        CleanExeVo result = null;
        try {
            result = cleanExeService.endResult(cleanExeDto);
        } catch (Exception e) {
            return AjaxResult.error(StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage());
        }
        return AjaxResult.success(result);
    }

    /**
     * 保存和修改公用
     * @param cleanExeDto  传递的实体
     * @return  null 失败  实体成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/_save")
    public AjaxResult cleanExeSave(@RequestBody CleanExeDto cleanExeDto) {
        CleanExeVo result = null;
        try {
            result = cleanExeService.updateResult(cleanExeDto);
        } catch (Exception e) {
            logger.error("cleanExeSave -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(result);
    }

    /**
     * 任务取消
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/cancelTask")
    public AjaxResult cancelCleanExe(@RequestBody CleanExeDto cleanExeDto){
        CleanExeVo result = null;
        try {
            result = cleanExeService.cancelResult(cleanExeDto);
        } catch (Exception e) {
            return AjaxResult.error(StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage());
        }
        return AjaxResult.success(result);
    }
    /**
     * 检索待审核任务
     * @return
     */
    @ResponseBody
    @GetMapping("/reviewExe")
    public AjaxResult getReviewExeList( CleanExeReviewDto cleanExeReviewDto){
        List<CleanExeReviewVo> result;
        try {
            result = cleanExeService.selectReviewExe(cleanExeReviewDto);
        } catch (Exception e) {
            return AjaxResult.error(StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage());
        }
        return AjaxResult.success(result);
    }
    /**
     * 保存审核结果
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/saveReviewExe")
    public AjaxResult cleanExeSave(@RequestBody CleanExeReviewDto cleanExeReviewDto) {
        CleanExeReviewDto result;
        try {
            result = cleanExeService.saveReviewExe(cleanExeReviewDto);
        } catch (Exception e) {
            return AjaxResult.error(StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage());
        }
        return AjaxResult.success(result);
    }

}